const mongoose = require('mongoose')
// 连接数据库
mongoose.connect('mongodb://127.0.0.1:27017/schoolRun')
    .then(res => {
        // console.log(res)
        console.log('数据库连接成功')
    })
    .catch(err => {
        // console.log(err)
        console.log('数据库链接错误')
    })
// 开启调试模式
mongoose.set('debug', true)

// 定义用户信息表规则
const userInfoSchema = new mongoose.Schema({
    openid: {
        type: String,
        unique: true
    },
    nickName: String,
    avatarUrl: String,
    gender: Number,
    phone: String,
    canReceive: {
        type: Boolean,
        default: false
    },
    personInfo: {
        type: Object,
        default: null
    }
})
// 创建模型
const user = mongoose.model('UserInfo', userInfoSchema, 'userInfo')

//建立用户地址信息表 
const addressSchema = new mongoose.Schema({
    openid: {
        type: String,
        required: true
    },
    building: String, //楼栋 
    doorNum: String, //门牌号 
    name: String, //收件姓名 
    phone: String, //收件电话 
    isDefault: { //默认地址 
        type: Boolean,
        default: false
    },
    addTime: String //添加事件 
})
//创建地址model对象 
const address = mongoose.model('Address', addressSchema, 'address')
//建立管理员信息表======================================================== 
const adminSchema = new mongoose.Schema({
    openid: String,
    username: String,
    password: String,
    nickName: String,
    role: String,
    time: String
})
const admin = mongoose.model('Admin', adminSchema, 'admin');
admin.create({
    openid: 'oh4hF5PeijMhdBy4ooVw9US9KlWo',
    username: 'admin',
    password: '123456',
    role: '超级管理员',
    nickName: '从前有座灵剑山',
    time: '2022-07-14 16:50:30'
})
//建立订单信息表 
const orderSchema = new mongoose.Schema({
    openid: String,
    startAddress: {
        type: String,
        default: ''
    },
    endAddress: {
        type: String,
        default: ''
    },
    orderType: String,
    payMoney: Number,
    publicTime: String,
    status: String,
    info: Object,
    userInfo: Object,
    acceptMan: {
        type: String,
        default: ''
    },
    acceptTime: {
        type: String,
        default: ''
    },
    confirmTime: {
        type: String,
        default: ''
    }
}) 
//创建地址model对象 
const order = mongoose.model('Order',orderSchema,'order') 
//导出 
module.exports = {
    user,
    address,
    admin,
    order
}